package com.example.wifi_direct.ui.main.fragment

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import cn.jzvd.Jzvd
import com.example.wifi_direct.R
import com.example.wifi_direct.ui.main.NewsApplication
import com.example.wifi_direct.ui.main.VideoListRvAdapter
import com.example.wifi_direct.ui.main.model.VideoModel
import com.example.wifi_direct.utils.Video
import com.example.wifi_direct.utils.showToast
import com.google.gson.Gson
import kotlinx.android.synthetic.main.fragment_video.*
import okhttp3.OkHttpClient
import okhttp3.Request
import kotlin.concurrent.thread

class VideoFragment : Fragment() {

    var dataList = mutableListOf<VideoModel>()


    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val view = inflater.inflate(R.layout.fragment_video, container, false)
        return view
    }

    /**
     * 视图创建完毕后调用
     */
    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        initData()
        initView()
    }


    fun initView() {
        // 给列表设置适配器
        recyclerview.layoutManager = LinearLayoutManager(activity, RecyclerView.VERTICAL, false)
        val videoListRvAdapter = VideoListRvAdapter(activity!!, dataList)
        recyclerview.adapter = videoListRvAdapter
        // 这里是为了性能和内存 当列表条目被回收时 销毁播放的控件
        recyclerview.addOnChildAttachStateChangeListener(object :
            RecyclerView.OnChildAttachStateChangeListener {
            override fun onChildViewAttachedToWindow(view: View) {}
            override fun onChildViewDetachedFromWindow(view: View) {
                val jzvd: Jzvd = view.findViewById(R.id.videoplayer)
                if (jzvd != null && Jzvd.CURRENT_JZVD != null &&
                    jzvd.jzDataSource.containsTheUrl(Jzvd.CURRENT_JZVD.jzDataSource.currentUrl)
                ) {
                    if (Jzvd.CURRENT_JZVD != null && Jzvd.CURRENT_JZVD.screen != Jzvd.SCREEN_FULLSCREEN) {
                        Jzvd.releaseAllVideos()
                    }
                }
            }
        })
    }

    // 模拟100条数据
    // 如果想要更改视频播放数据源或者封面等 在创建VideoModel对象时键入对应的值就可以
    fun initData() {
        dataList = Video.videoList
//        repeat(5) {
//            dataList.add(VideoModel(
//                "蓝色战衣",
////                "https://oss.ghovos.top/content/xiaoshazi.mp4",
//                "https://f.video.weibocdn.com/IjpY78Rklx07NFWR8mUg01041201TPB80E010.mp4?label=mp4_1080p&template=1920x1080.25.0&trans_finger=d88af6227b88881484d2b59dfbafa136&media_id=4651194157826056&tp=8x8A3El:YTkl0eM8&us=0&ori=1&bf=3&ot=h&ps=3lckmu&uid=6z9i9k&ab=3915-g1,966-g1,1493-g0,1192-g0,1191-g0,1258-g0&Expires=1624456511&ssig=XKRjBj0mS5&KID=unistore,video",
//                "http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20200105/c5162a9d6f484ce3b8ff464b27f8865f.jpeg",
//                "http://5b0988e595225.cdn.sohucs.com/q_70,c_zoom,w_640/images/20200105/c5162a9d6f484ce3b8ff464b27f8865f.jpeg",
//                "条目标题",
//                "条目描述"
//            )
//
//
//            )
//
//        }

    }

    // 当页面暂停交互时 销毁播放控件实例
    override fun onPause() {
        super.onPause()
        Jzvd.releaseAllVideos()
    }
}